/*
 * File:   ParametersDialog.cpp
 * Author: Gab'z
 *
 * Created on 6 juin 2012, 16:42
 */

#include "ParametersDialog.h"
#include "../model/parameters/Parameters.h"
#include <iostream>
#include <string>
namespace calculator {

    ParametersDialog::ParametersDialog(const Engine* engine, QWidget* parent) :
    QDialog(parent), _engine(engine) {
        widget.setupUi(this);
        widget.visibleStackSizeSpinBox->setMinimum(MIN_VISIBLE_STACK_SIZE);
        widget.visibleStackSizeSpinBox->setMaximum(MAX_VISIBLE_STACK_SIZE);
        int constantType = _engine->getContext()->getParameters()->getConstantType();
        int angleUnit = _engine->getContext()->getParameters()->getAngleUnit();

        widget.constantTypeComboBox->setCurrentIndex(constantType);
        widget.angleUnitComboBox->setCurrentIndex(angleUnit);
        widget.complexCheckBox->setChecked(_engine->getContext()->getParameters()->getComplexMode());
        widget.displayKeyboardParameter->setChecked(_engine->getContext()->getParameters()->getDisplayKeyboard());
        widget.visibleStackSizeSpinBox->setValue((int) _engine->getContext()->getParameters()->getVisibleStackSize());
        widget.instantComputeCheckBox->setChecked(_engine->getContext()->getParameters()->getInstantCompute());
        widget.integerDivisionCheckBox->setChecked(_engine->getContext()->getParameters()->getIntegerDivision());
        widget.saveOnExitCheckBox->setChecked(_engine->getContext()->getParameters()->getSaveOnExit());

        connect(widget.cancelButton, SIGNAL(clicked()), this, SLOT(sCancel()));
        connect(widget.saveButton, SIGNAL(clicked()), this, SLOT(sSave()));
    }

    ParametersDialog::~ParametersDialog() {
    }

    void ParametersDialog::sSave() {
        Parameters::ConstantType constantType;
        switch (widget.constantTypeComboBox->currentIndex()) {
            case 0:
            {
                constantType = calculator::Parameters::INTEGER;
                break;
            }
            case 1:
            {
                constantType = calculator::Parameters::RATIONNAL;
                break;
            }
            case 2:
            {
                constantType = calculator::Parameters::REAL;
                break;
            }
            default:
            {
                // choisir integer par défaut
                constantType = calculator::Parameters::INTEGER;
                break;
            }
        }
        Parameters::AngleUnit angleUnit;
        switch (widget.angleUnitComboBox->currentIndex()) {
            case 0:
            {
                angleUnit = calculator::Parameters::DEGREE;
                break;
            }
            case 1:
            {
                angleUnit = calculator::Parameters::RADIAN;
                break;
            }
            default:
            {
                // choisir degree par défaut
                angleUnit = calculator::Parameters::DEGREE;
                break;
            }
        }
        const Parameters* parameters = new Parameters(
                constantType,
                angleUnit,
                widget.complexCheckBox->isChecked(),
                widget.displayKeyboardParameter->isChecked(),
                (unsigned int) widget.visibleStackSizeSpinBox->value(),
                widget.instantComputeCheckBox->isChecked(),
                widget.integerDivisionCheckBox->isChecked(),
                widget.saveOnExitCheckBox->isChecked());
        _engine->saveAllParameters(parameters);
        delete parameters;
        close();
    }

    void ParametersDialog::sCancel() {
        close();
    }
}
